import {HeartModel} from '../../../model/heart'
import {uploadCOSTxCloud} from '../../../utils/cloud'
var heart = new HeartModel()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    contentText:'',
    content:0
  },
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);
    this.setData({
      contentText:value,
      content:len
    })
  },
  submit(){
    var images = this.data.imgs.join(";;")
    if(this.data.content === 0){
      wx.showToast({
        title: '心声内容不能为空',
        icon: 'none'
      })
    }else{
          heart.insert(this.data.contentText,images,0).then(
      res=>{
        if (res.code === 0) {
          wx.navigateBack({})
        }
      }
    )
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  chooseImg(){
      let that = this;
      let len = this.data.imgs;
      if(len>=9){
          this.setData({
              lenMore: 1
          })
         

          return;
      }
      wx.chooseImage({
        count: 9, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'],
          success: (res)=>{
            console.log(res)
              let tempFilePaths = res.tempFilePaths;
              let imgs = that.data.imgs;
              for(let i=0;i<tempFilePaths.length;i++){
                      uploadCOSTxCloud('','voice',tempFilePaths[i]).then(
                        res=>{
                          if(imgs.length<9){
                            imgs.push(res)
                          that.setData({
                          imgs
                      })}else{
                        that.setData({
                            imgs
                        })
                        wx.showModal({
                            title: '提示',
                            content: '最多只能有九张图片'
                        })
                        return;
                    }
                        }
                      )                      
              }
              that.setData({
                  imgs
              })
          }
      })
  },
  previewImg(e){
      let index = e.currentTarget.dataset.index;
      let imgs = this.data.imgs;
      wx.previewImage({
          current: imgs[index],
          urls: imgs,
      })
  },
  deleteImg(e){
      let _index = e.currentTarget.dataset.index;
      let imgs = this.data.imgs;
      imgs.splice(_index,1);
      this.setData({
          imgs
      })
  }

})